export const listMixin = {
  data() {
    return {
      // 遮罩层
      loading: true,
      //表格高度
      tableHeight: 250,
      //所有列表数据
      list: null,
      //总行数
      total: 0,
      //进度条
      listLoading: true,
      //查询参数
      queryParams: {
        //第几页
        pageNum: 1,
        //一页几行
        pageSize: 20,
        //条件
        params: {
          dates: []
        },
      },
      //表单对象
      form: {
        id: undefined,
      },
      // 选中数组
      ids: [],
      // 选中列
      rows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //弹框状态
      dialogStatus: "",
      //弹框显示
      dialogFormVisible: false,
      baseUrl: process.env.VUE_APP_BASE_API,
    };
  },
  created() {
    this.tableHeight = window.innerHeight - 270;
  },
  mounted() {
    const _this = this;
    window.onresize = () => {
      return (() => {
        let heightStyle = window.innerHeight - 270;
        _this.tableHeight = heightStyle;
      })();
    };
  },
  methods: {
    //获取数据
    fetchData() {
      //开启进度条
      this.loading = true;
      let data = {
        ...this.queryParams,
      };
      //开始，结束时间赋值
      if (data.params.dates) {
        this.addDateRange(data, data.params.dates)
      }
      //调用list接口
      this.callList(data).then(res => {
        this.list = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    //搜索
    handleFilter() {
      this.queryParams.pageNum = 1;
      this.fetchData();
    },
    //点击添加
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    //点击编辑
    handleUpdate(row) {
      this.resetTemp();
      const id = row.id || this.ids;
      this.callFindById(id).then(res => {
        this.form = res.data;
        this.dialogStatus = "update";
        this.dialogFormVisible = true;
      });
    },
    //保存成功
    saveSuccess() {
      this.dialogFormVisible = false;
      if (this.dialogStatus === "create") {
        this.handleFilter();
      } else {
        const index = this.list.findIndex((v) => v.id === this.form.id);
        this.list.splice(index, 1, this.form);
      }
    },
    //点击删除
    handleDelete(row, index) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.callDelete(row.id).then(res => {
          this.handleFilter();
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        });
      })
    },
    //点击审核
    handleAudit(row, index) {
      this.callAudit(row.id).then(res => {
        row.status = 1;
        this.$notify({
          title: "成功",
          message: "审核 成功",
          type: "成功",
          duration: 2000,
        });
      });
    },
    //点击弃审
    handleUnAudit(row, index) {
      this.callUnAudit(row.id).then(res => {
        row.status = 0;
        this.$notify({
          title: "成功",
          message: "弃审 成功",
          type: "成功",
          duration: 2000,
        });
      });
    },
    //多选变化
    handleSelectionChange(selection) {
      this.rows = selection;
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    //批量删除
    handleDeletes() {
      if (this.ids && this.ids.length > 0) {
        this.$confirm('此操作将永久删除选择的数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.callDeletes(this.ids).then(res => {
            this.handleFilter();
            this.$notify({
              title: "成功",
              message: "删除 成功",
              type: "成功",
              duration: 2000,
            });
          });
        })
      } else {
        this.$notify({
          title: "失败",
          message: "请选择要删除的项",
          type: "失败",
          duration: 2000,
        });
      }
    },
    //重置表单数据
    resetTemp() {
      this.form = {
        id: undefined,
      };
    },
    //重置搜索
    resetSearch() {
      let params = this.queryParams.params;
      let keys = Object.keys(params);
      for (let i = 0; i < keys.length; i++) {
        params[keys[i]] = "";
      }
    },
    //导出excel
    exportExcel() {
      this.callExport(this.queryParams).then(res => {

      });
    }
  }
};
